package com.baizhi.service.impl;
import com.baizhi.dao.UserDao;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.util.ThreadPollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
@Slf4j
//log.debug()
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    //查询用户  分页查询+模糊查询
    @Override
    public Map listUser(int page, int limit, String name) {

        int start=(page-1)*limit;
        List<User> userList = userDao.listUser(start, limit, name);
        int count=userDao.countUser(name);
        Map map=new HashMap();
        map.put("data",userList);
        map.put("count",count);
        map.put("code",0);
        map.put("msg","");
        log.debug("查询用户");
        return map;
    }

    //查看所有用户
    @Override
    public List<User> listUser2() {
        return userDao.listUser2();
    }

    //修改用户状态  冻结或者解冻
    @Override
    public Map updateUser(Integer id, Integer status) {
        int i = userDao.updateUser(id, status);
        Map map=new HashMap();
        if(i>0){
            map.put("updateUser","修改成功！");
        }else{
            map.put("updateUser","修改失败！");
        }
        return map;
    }



//    优化前   优化前后都是使用一个ajax发送请求
    public Map selectByAll(){
        Map map=new HashMap();
//                男女用户人数对比统计
        List<Map> selectBySexCount = userDao.selectBySexCount();
        map.put("selectBySexCount",selectBySexCount);
//                过去三周用户注册量变化
        map.put("one",userDao.selectByDayCount(7,1));
        map.put("two",userDao.selectByDayCount(14,7));
        map.put("three",userDao.selectByDayCount(21,14));
//                用户省份人数对比
        List<Map> selectByProCount = userDao.selectByProCount();
        map.put("selectByProCount",selectByProCount);
        return map;
    }



//    优化后--多线程多任务并行执行
    public Map selectByThread() {

       final Map map=new HashMap();
//        1.创建计数器
        final CountDownLatch countDownLatch = new CountDownLatch(3);
//        2.开启三个线程的任务
        final ThreadPoolExecutor executor = ThreadPollUtil.executor;
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                男女用户人数对比统计
                List<Map> selectBySexCount = userDao.selectBySexCount();
                map.put("selectBySexCount",selectBySexCount);
//                计数器减一
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                过去三周用户注册量变化
                map.put("one",userDao.selectByDayCount(7,1));
                map.put("two",userDao.selectByDayCount(14,7));
                map.put("three",userDao.selectByDayCount(21,14));
//                计数器减一
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
//                用户省份人数对比
                List<Map> selectByProCount = userDao.selectByProCount();
                map.put("selectByProCount",selectByProCount);
//                计数器减一
                countDownLatch.countDown();
            }
        });


//        3.使用await()方法，让主线程等待任务线程执行结束
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return map;
    }

//    优化前后使用了同一个ajax请求，只不过优化前是单线程，优化后使用多线程。

}
